package com.example.lhh.purchase.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.lhh.common.LhhResponseJson;
import com.example.lhh.purchase.entity.LhhMedicine;
import com.example.lhh.purchase.service.ILhhMedicineService;
import com.example.lhh.purchase.vo.LhhMedicineVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  药品信息 前端控制器
 * </p>
 *
 * @author luoHongHu
 * @since 2025-05-01
 */
@RestController
@RequestMapping("/lhhMedicine")
public class LhhMedicineController {

    private static final Logger log = LoggerFactory.getLogger(LhhMedicineController.class);
    @Autowired
    private ILhhMedicineService lhhMedicineService;

    /**
     * 添加药品信息
     * @param lhhMedicine
     * @return
     */
    @PostMapping("/addMedicine")
    public LhhResponseJson addMedicine(@RequestBody LhhMedicine lhhMedicine) {
        List<LhhMedicine> lhhMedicineList = null;
        try{
            lhhMedicineService.addMedicine(lhhMedicine);
            return new LhhResponseJson("200",true,"添加成功");
        }catch (Exception e){
            e.printStackTrace();
        }
        return new LhhResponseJson("400",false,"添加成功");
    }

    /**
     * 获取药品信息
     * @param lhhMedicineVo
     * @param pageNum 当前页
     * @param pageSize 每页条数
     * @return
     */
    @PostMapping("/getMedicineList")
    public LhhResponseJson getMedicineList(@RequestParam("pageNum") int pageNum, @RequestParam("pageSize") int pageSize,
                                           @RequestBody LhhMedicineVo lhhMedicineVo) {
        IPage<LhhMedicine> lhhMedicineList = null;
        try{
            lhhMedicineList =  lhhMedicineService.getMedicineList(lhhMedicineVo,pageNum,pageSize);
            return new LhhResponseJson("200",true,"查询成功",lhhMedicineList);
        }catch (Exception e){
            e.printStackTrace();
        }
        return new LhhResponseJson("200",true,"查询成功",lhhMedicineList);
    }

    @DeleteMapping("/delMedicineById")
    public LhhResponseJson delMedicineById(@RequestParam("id") String id) {
        log.info("id",id);
        try {
            if(StringUtils.isEmpty(id)){
                return new LhhResponseJson<>(false,"传入的ID不能为空！");
            }
            lhhMedicineService.delMedicineById(id);
            return new LhhResponseJson<>(true,"删除成功！");
        }catch (Exception e){
            e.printStackTrace();
        }
        return new LhhResponseJson<>(false,"删除失败！");
    }
}
